<?php
/**
 * Model_CarModel
 *  
 * @author x
 * @version 
 */

class Model_CarModel extends Zend_Db_Table_Abstract
{
    /**
     * The default table name
     */
    protected $_name = 'tcl_car_model';
    
    public function getAllBrand() {
        
        $db = $this->getAdapter();
        $query = $db->query(
        		"SELECT id,s_py FROM $this->_name WHERE level = 1 GROUP BY s_py"
        		);
        
        $results = array();
        $i = 0;
        while ($value = $query->fetch()) {
        	$results[]                  = $value;
        	$results[$i][id]            = $value[id];
        	$results[$i][s_py]          = $value[s_py];
        	$query2 = $db->query("SELECT id,brand FROM $this->_name WHERE s_py = '$value[s_py]' AND level = 1");
        	$m = 0;
        	while ($value2 = $query2->fetch()){
        	    $results[$i][arr][$m][id]			= $value2[id];
        	    $results[$i][arr][$m][brand]		= $value2[brand];
        	    $m++;
        	}
        	
        	$i++;
        }
        //$results = $this->fetchAll("level = 1");
        
        
        //print_r($results[arr]);
        //exit();
        
        return $results;
    }
    

    
    public function getModelName($brand_id) {
        
        
        if ($brand_id) {
	     	$db = $this->getAdapter();
	    	$where = $db->quoteInto("id = ?", $brand_id);
	    	$results = $this->fetchRow($where)->toArray();
        }

    	
    	return $results;
    }
    
    
    public function getModel($brand_id) {

        $db = $this->getAdapter();
        $where = $db->quoteInto("p_id = ?", $brand_id)
        		.$db->quoteInto(' AND level = ?', '2');
        $model = $this->fetchAll($where)->toArray();
        
        //print_r($company);

        foreach ($model as $k=>$company_value){
            $where = $db->quoteInto(' p_id = ?', $company_value[id])
                    .$db->quoteInto(' AND level = ?', '3');
            $model[$k][model] = $this->fetchAll($where)->toArray();
        }
        
        return $model;
    }
    
    
    public function getCarname($model_id){
        $db = $this->getAdapter();
        $where = $db->quoteInto("p_id = ?", $model_id)
        		.$db->quoteInto(' AND level = ?', '4');
        $carname = $this->fetchAll($where)->toArray();
        
        return $carname;
    }
}
